package com.yunhai.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.common.utils.ReflectionCopy;
import com.yunhai.device.mapper.FdsEarlywarningRulesetMapper;
import com.yunhai.device.service.FdsEarlywarningRulesetService;
import com.yunhai.model.entity.fds.FdsEarlywarningRulesetEntity;
import com.yunhai.model.vo.fds.FdsEarlywarningRulesetQuery;
import com.yunhai.model.vo.fds.FdsEarlywarningRulesetVo;
import org.apache.commons.beanutils.PropertyUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FdsEarlywarningRulesetServiceImpl implements FdsEarlywarningRulesetService {

    @Resource
    private FdsEarlywarningRulesetMapper fdsEarlywarningRulesetMapper;

    /**
     * 获取 FdsEarlywarningRuleset 分页列表
     */
    @Transactional
    public IPage getFdsEarlywarningRulesetPagination(PaginationVo paginationVo, FdsEarlywarningRulesetQuery fdsEarlywarningRulesetQuery ) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<FdsEarlywarningRulesetEntity> lqs = new LambdaQueryWrapper();
        fdsEarlywarningRulesetMapper.selectPage(page, lqs);

        List<FdsEarlywarningRulesetEntity> record = page.getRecords();
        if( !ObjectUtil.isNotEmptyList(record)){
            return page;
        }
        return page;
    }


    /**
     * 获取 FdsEarlywarningRuleset 所有数据
     *
     */
    @Transactional
    public List<FdsEarlywarningRulesetEntity> getFdsEarlywarningRulesetAllList( FdsEarlywarningRulesetQuery fdsEarlywarningRulesetQuery ) throws Exception {

        LambdaQueryWrapper<FdsEarlywarningRulesetEntity> lqs = new LambdaQueryWrapper();
        List<FdsEarlywarningRulesetEntity> list = fdsEarlywarningRulesetMapper.selectList(lqs);
        return list;
    }


    /**
     * 获取 FdsEarlywarningRuleset 详情
     */
    @Transactional
    public FdsEarlywarningRulesetEntity getFdsEarlywarningRulesetDetail( Long id) throws Exception {

        FdsEarlywarningRulesetEntity fdsEarlywarningRulesetEntity = fdsEarlywarningRulesetMapper.selectById(id);
        return fdsEarlywarningRulesetEntity;
    }


    /**
     * 新增 FdsEarlywarningRuleset
     * */
    @Transactional
    public void addFdsEarlywarningRuleset(FdsEarlywarningRulesetVo fdsEarlywarningRulesetVo) throws Exception {

        FdsEarlywarningRulesetEntity fdsEarlywarningRulesetEntity = new FdsEarlywarningRulesetEntity();
        ReflectionCopy.copyProperties( fdsEarlywarningRulesetEntity, fdsEarlywarningRulesetVo );
        fdsEarlywarningRulesetMapper.insert(fdsEarlywarningRulesetEntity);
        return;
    }


    /**
     * 更新 FdsEarlywarningRuleset
     * */
    @Transactional
    public void updateFdsEarlywarningRuleset( FdsEarlywarningRulesetVo fdsEarlywarningRulesetVo) throws Exception {

        if( fdsEarlywarningRulesetVo.getId() == null ){
            throw GlobalException.gen(10021, "id不存在");
        }
        FdsEarlywarningRulesetEntity fdsEarlywarningRulesetEntity = new FdsEarlywarningRulesetEntity();
        ReflectionCopy.copyProperties( fdsEarlywarningRulesetEntity, fdsEarlywarningRulesetVo );
        fdsEarlywarningRulesetMapper.updateById(fdsEarlywarningRulesetEntity);
        return;
    }

    /**
     * 删除 FdsEarlywarningRuleset
     * */
    @Transactional
    public void batchRemoveFdsEarlywarningRuleset( List<Long> ids ) throws Exception {

        if( !ObjectUtil.isNotEmptyList(ids) ){
            throw GlobalException.gen(10011, "要删除的id不存在");
        }
        fdsEarlywarningRulesetMapper.deleteBatchIds(ids);
    }

}